<?php
/*
 * @FileForDetails: 后台控制器基类
 * @Author: zxr
 * @Date: 2022-12-10 16:55:09
 */
namespace app\common\controller;

use think\Controller;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Backend extends Controller
{
    /**
     * 模型对象
     * @var \think\Model
     */
    protected $model = null;

    /**
     * @Descripttion: 软删除
     * @Author: zxr
     * @Date: 2022-12-10 11:40:25
     */    
    public function del()
    {
        // 1.验证请求方式
        if ($this->request->isGet()) {
            // 2.获取请求数据
            $id = $this->request->get('id');

            $count = 0;
            Db::startTrans();
            try {
                $count++;
                // 4.数据库操作
                $this->model::destroy($id);
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

            return $count > 0 ? json(['code' => 1, 'msg' => 'Yes, you deleted a line']) : json(['code' => 0, 'msg' => 'No rows were deleted']);
        }
    }

    /**
     * @Descripttion: 真实删除
     * @Author: zxr
     * @Date: 2022-12-10 11:40:25
     */
    public function trueDel()
    {
        if ($this->request->isPost()) {
            $ids = $this->request->post('id');
            $count = 0;
            Db::startTrans();
            try {
                $count++;
                if (is_array($ids)) {
                    foreach($ids as $val) {
                        $shops = $this->model::onlyTrashed()->find($val);
                        $shops->restore();
                        $this->model::destroy($val, true);
                    }
                }
                if (empty($ids)) {
                    $data = $this->model::onlyTrashed()->select();
                    foreach($data as $val) {
                        $shops = $this->model::onlyTrashed()->find($val['id']);
                        $shops->restore();
                        $this->model::destroy($val['id'], true);
                    }
                }
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }
            return $count > 0 ? json(['code' => 1, 'msg' => 'Yes, you deleted a line']) : json(['code' => 0, 'msg' => 'No rows were deleted']);
        }

    }

    /**
     * @Descripttion: 还原
     * @Author: zxr
     * @Date: 2022-12-10 10:24:37
     */ 
    public function restore()
    {
        if ($this->request->isPost()) {
            $ids = $this->request->post('id');
            $count = 0;
            Db::startTrans();
            try {
                $count++;
                if (is_array($ids)) {
                    foreach($ids as $val) {
                        $shops = $this->model::onlyTrashed()->find($val);
                        $shops->restore();
                    }
                }
                if(is_string($ids) && !empty($ids)) {
                    $shops = $this->model::onlyTrashed()->find($ids);
                    $shops->restore();
                }
                if (empty($ids)) {
                    $data = $this->model::onlyTrashed()->select();
                    foreach($data as $val) {
                        $shops = $this->model::onlyTrashed()->find($val['id']);
                        $shops->restore();
                    }
                }
                Db::commit();
            } catch (ValidateException|PDOException|Exception $e) {
                Db::rollback();
                $this->error($e->getMessage());
            }

            return $count > 0 ? json(['code' => 1, 'msg' => 'success']) : json(['code' => 0, 'msg' => 'error']);
        }
    }

    /**
     * @Descripttion: 遍历回收站
     * @Author: zxr
     * @Date: 2022-12-10 10:24:37
     */    
    public function recyleList()
    {
        $curr = $_GET['page'] ?? 1;
        $limit = $_GET['limit'] ?? 5;
        $start = ($curr - 1) * $limit;
        $list = $this->model::onlyTrashed()->limit($start, $limit)->order('delete_time','desc')->select();
        return json(['code' => 0, 'msg' => 'success', 'count' => $this->model::onlyTrashed()->count(), 'data' => $list]);
    }

    /**
     * @Descripttion: 渲染回收站
     * @Author: zxr
     * @Date: 2022-12-10 10:22:19
     */    
    public function recycle()
    {
        return $this->fetch();
    }
    
    /**
     * @Descripttion: 渲染增加
     * @Date: 2022-11-30 15:53:49
     */    
    public function add()
    {  
        return $this->fetch();
    }

    /**
     * @Descripttion: 渲染修改
     * @Date: 2022-11-30 15:53:49
     */    
    public function edit()
    {
        return $this->fetch();
    }

    /**
     * @Descripttion: 渲染上传
     * @Date: 2022-11-25 18:50:14
     */    
    public function upload()
    {
        return $this->fetch();
    }

    /**
     * @Descripttion: 渲染主页面
     * @Date: 2022-11-25 18:50:14
     */    
    public function index()
    {
        return $this->fetch();
    }
}